//
//  CZMainViewController.swift
//  Weibo
//
//  Created by 铭铭 on 15/11/22.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class CZMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //首页
        let homeVC = CZHomeViewController()
        addChildViewController(homeVC, title: "首页", Name: "tabbar_home")
        

        //消息
        let MessageVC = CZMessageViewController()
        addChildViewController(MessageVC, title: "消息", Name:"tabbar_message_center")
        
        
        //空的占位控制器
        addChildViewController(UIViewController())
        
        
        // 发现
        let discoverVC = CZDiscoverViewController()
        addChildViewController(discoverVC, title: "发现", Name: "tabbar_discover")
        
        //我
        let ProfileVC = CZProfileViewController()
        addChildViewController(ProfileVC, title: "我", Name: "tabbar_profile")
       
    
    }

    /*抽取，自定义添加tabbar子控制器
    - parameter controller: 子控制器
    - parameter title:      子控制器的标题
    - parameter imageName:  子控制器的图片名称
    */
    func addChildViewController(controller:UIViewController,title:String,Name: String){
        
        controller.title = title
        
        controller.tabBarItem.image = UIImage(named:Name)
        
        //设置原文字效果
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: UIControlState.Highlighted)
        
        // 拼接高亮图片名称
        let highlightedImageName = Name + "_highlighted"
        
        let highlightedImage = UIImage(named: highlightedImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        controller.tabBarItem.selectedImage = highlightedImage
        
        //将控制器添加到根控制器中
        addChildViewController(UINavigationController(rootViewController: controller))
        
    }
    //设置➕按钮的位置
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        let width = tabBar.bounds.size.width/5
        
        let frame = CGRect(x: 2 * width - 5 , y: 0, width: width + 10, height: tabBar.bounds.height)
        
        
        composeButton.frame=frame
        
    }
    
    //懒加载
    //➕按钮
    lazy var composeButton: UIButton = {
        let button = UIButton()
        
        //设置参数
        //设置按钮背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        //设置按钮高亮图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        // 设置按钮图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        // 添加到tabbar里面
        self.tabBar.addSubview(button)
    
        return button
    }()
    
    
    
    
}
